// pages/carts/index/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: []
  },
  addOrder(){
    let that = this
    let list = this.data.list
    let pkIds = []
    list.forEach(item=>pkIds.push(item.pkId))
   

    let obj = {
      url: `/system/carts/addOrder/${pkIds}`,
      method: "post"
    }
    app.request.wxRequest(obj).then(res => {
      if (res.data.code == 200) {


     let orderId = res.data.data
        wx.showModal({
          title: '提示',
          content: '微信支付',
          success(res) {
            if (res.confirm) {


              let obj = {
                url: "/system/orders",
                method: "put",
                data: {
                  "orderId": orderId, //1
                  "status":2
                }
              }
              app.request.wxRequest(obj).then(res => {
                if (res.data.code == 200) {
                  wx.showToast({
                    title: '支付成功！',
                    duration: 500,
                    success: () => {
                      that.getList()
                      wx.switchTab({
                        url: '/pages/orders/index/index',
                      })
                    }
                  })

                }
              })

            } else if (res.cancel) {}
          }
        })
      }
    })
  },
  delete(e) {
    let index = e.currentTarget.dataset.index
    let item = e.currentTarget.dataset.item

    let pkIds = [item.pkId]

    let obj = {
      url: `/system/carts/${pkIds}`,
      method: "delete"
    }
    app.request.wxRequest(obj).then(res => {
      if (res.data.code == 200) {
        this.getList()
      }
    })

  },
  onChangeStepper(e) {
    let gdsQty = e.detail
    let index = e.currentTarget.dataset.index
    let pkId = e.currentTarget.dataset.item.pkId
    this.setData({
      [`list[${index}].gdsQty`]: gdsQty
    })
    let obj = {
      url: "/system/carts",
      method: "put",
      data: {
        "pkId": pkId, //1
        "gdsQty": gdsQty //数量
      }
    }
    app.request.wxRequest(obj).then(res => {
      if (res.data.code == 200) {
        this.getList()
      }
    })
  },
  getList() {
    let obj = {
      url: "/system/carts/list",
      method: "get",
    }
    app.request.wxRequest(obj).then(res => {
      if (res.data.code == 200) {
        let list = res.data.rows
        this.setData({
          list: list,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    that.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})